package com.jnxx.task.service.impl;

import com.jnxx.basic.service.impl.BaseServiceImpl;
import com.jnxx.basic.util.GetStaffRequestUtil;
import com.jnxx.task.domain.*;
import com.jnxx.task.domain.Module;
import com.jnxx.task.mapper.*;
import com.jnxx.task.service.IModuleService;
import com.jnxx.task.service.IOkrprojectService;
import com.jnxx.task.service.IProjectandstaffService;
import com.jnxx.task.vo.GetProgressDetailVo;
import com.jnxx.task.vo.ProjectDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 业务实现类：
 */
@Slf4j
@Service
public class OkrprojectServiceImpl extends BaseServiceImpl<Okrproject> implements IOkrprojectService {

    @Autowired
    private ModuleMapper moduleMapper;

    @Autowired
    private OkrcardMapper cardMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private OkrprojectMapper okrprojectMapper;

    @Autowired
    private IModuleService moduleService;

    @Autowired
    private IProjectandstaffService projectandstaffService;

    @Autowired
    private ProjectandstaffMapper projectandstaffMapper;

    @Autowired
    private OkrStaffMapper staffMapper;

    @Override
    public void add(Okrproject okrproject) {
        super.add(okrproject);
        String moduleName = okrproject.getModuleName();
        Module module = new Module();
        module.setModuleName(moduleName);
        module.setProjectId(Long.valueOf(okrproject.getId()).intValue());
        moduleMapper.add(module);

        Integer[] staffIds = okrproject.getIds();
        OkrStaff staff = GetStaffRequestUtil.getStaff();
        for (Integer staffId : staffIds) {
            Projectandstaff projectandstaff = new Projectandstaff();
            projectandstaff.setCreatId(Math.toIntExact(staff.getId()));
            projectandstaff.setProjectId(Math.toIntExact(okrproject.getId()));
            projectandstaff.setStaffId(staffId);
            projectandstaffService.add(projectandstaff);
        }
    }


    @Override
    public ProjectDetailVo getDetaile(Map<String, GetProgressDetailVo> idMap) {
        GetProgressDetailVo getProgressDetailVo = idMap.get("ids");
        Long id = getProgressDetailVo.getProjectId();
        Long moduleId = getProgressDetailVo.getModuleId();

        // 获取到项目
        Okrproject okrproject = super.findOne(id);
        // 根据项目id获取响应模块
        List<Module> modules = moduleMapper.findByProgrectId(id);
        // 根据模块id获取卡片
        if (moduleId == null){
            Module module = modules.get(0);
            moduleId = module.getId();
        }

        List<Okrcard> okrcards = cardMapper.findByModuleId(moduleId);

        for (Okrcard okrcard : okrcards) {
            List<Task> taskList = taskMapper.findByCardId(okrcard.getId());

            if (taskList.size()> 0){
                int totalTask = taskList.size();
                long completeTask = taskList.stream().filter(Task::getStates).count();
                okrcard.setTotalTask(totalTask);
                okrcard.setCompleteTask(completeTask);
                okrcard.setTaskList(taskList);
            }
        }

        return new ProjectDetailVo(okrproject, modules, okrcards);
    }

    /**
     * 根据员工id查询项目
     * @param staffId
     * @return
     */
    @Override
    public List<Okrproject> findAllByStaffId(Long staffId) {
        List<Projectandstaff> projectandstaffs = projectandstaffMapper.findByStaffId(staffId);
        Integer[] ids = projectandstaffs.stream().map(Projectandstaff::getProjectId).toArray(Integer[]::new);
        List<Okrproject> list = okrprojectMapper.findByIds(ids);
        return list;
    }


    @Override
    public void del(Long id) {
        List<Module> modules = okrprojectMapper.findModuleById(id);
        for (Module module : modules) {
            moduleService.del(module.getId());
        }
        Okrproject okrproject = super.findOne(id);
        OkrStaff staff = GetStaffRequestUtil.getStaff();
        projectandstaffMapper.delByProjectId(id);
        log.info("{}删除了名字为：{}，的项目", staff.getName(), okrproject.getProjectName());
        super.del(id);
    }

    @Override
    public Okrproject findOne(Long id) {
        List<Projectandstaff> projectandstaffs =projectandstaffMapper.findByProjectId(id);
        List<Integer> collect = projectandstaffs.stream().map(Projectandstaff::getStaffId).collect(Collectors.toList());
        List<OkrStaff> staffList = staffMapper.findByIds(collect);
        Okrproject okrproject = super.findOne(id);
        okrproject.setStaffList(staffList);
        return okrproject;
    }

    @Override
    public void update(Okrproject okrproject) {
        Integer[] ids = okrproject.getIds();
        Long id = okrproject.getId();
        OkrStaff staff = GetStaffRequestUtil.getStaff();
        // 先删除
        projectandstaffMapper.delByProjectId(id);
        // 再增加，判断有没有
        if (ids.length >0 ){
            for (Integer staffId : ids) {
                Projectandstaff projectandstaff = new Projectandstaff();
                projectandstaff.setStaffId(staffId);
                projectandstaff.setProjectId(Math.toIntExact(id));
                projectandstaff.setCreatId(Math.toIntExact(staff.getId()));
                projectandstaffMapper.add(projectandstaff);
            }
        }
        super.update(okrproject);
    }


}
